<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>详情</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list"
     style="padding: 20px 30px 0 0;">
    <input type="hidden" id="entityId"/>
    <div class="layui-form-item">
        <label class="layui-form-label">商品编码</label>
        <div class="layui-input-inline">
            <input type="text" id="product_number" lay-verify="required" placeholder="请输入商品编码" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">供应商</label>
        <div class="layui-input-inline">
            <select id="supplier_id" lay-filter="supplier_id" lay-verify="required">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品类型</label>
        <div class="layui-input-inline">
            <select id="product_type_id" lay-filter="product_type_id" lay-verify="required">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-inline">
            <input type="text" id="name" lay-verify="required" placeholder="请输入商品类型名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">售价</label>
        <div class="layui-input-inline">
            <input type="text" id="price" lay-verify="required|number" placeholder="请输入商品售价" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">进价</label>
        <div class="layui-input-inline">
            <input type="text" id="inprice" lay-verify="required|number" placeholder="请输入商品进价" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品单位</label>
        <div class="layui-input-inline">
            <input type="text" id="unit" lay-verify="required" placeholder="请输入商品单位/个" autocomplete="off"
                   class="layui-input" value="个">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">介绍</label>
        <div class="layui-input-inline">
            <input type="text" id="spec" lay-verify="required" placeholder="请输入商品介绍" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-inline">
            <input type="text" id="remark" lay-verify="required" placeholder="请输入备注信息" autocomplete=" off"
                   class="layui-input" value=" ">
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="entity-submit" id="entity-submit"
               value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit"
               value="确认编辑">
    </div>
</div>
<script src="../../layuiadmin/layui/layui.js"></script>
<script src="../../js/jquery-1.8.3.min.js"></script>
<script>
    //下拉框初始化
    $.ajax({
        url: '/product/findTerm',
        type: 'post',
        dataType: 'json',
        async: false,//设置阻断
        data: {},
        success: function (data) {
            if (data.status != 200) {
                layer.msg('查询失败！');
            } else {
                //将下拉框中添加数据
                var modelList = data.productType;
                if (modelList && modelList.length != 0) {
                    for (var i = 0; i < modelList.length; i++) {
                        var option = "<option value=\"" + modelList[i].id + "\"";
                        option += ">" + modelList[i].name + "</option>";  //动态添加数据
                        $('#product_type_id').append(option);
                    }
                }
                var modelList1 = data.supplierType;
                if (modelList1 && modelList1.length != 0) {
                    for (var i = 0; i < modelList1.length; i++) {
                        var option = "<option value=\"" + modelList1[i].id + "\"";
                        option += ">" + modelList1[i].name + "</option>";  //动态添加数据
                        $('#supplier_id').append(option);
                    }
                }
            }
        }
    });
    //根据父级页面判断，如果是修改，则添加默认值
    if (parent.entityType == "toUpdate") {
        $.ajax({
            url: parent.toUpdateURL,
            type: 'post',
            dataType: 'json',
            async: false,//设置阻断
            data: {"id": parent.entityId},
            success: function (data) {
                $("#entityId").val(parent.entityId);
                $("#product_number").val(data.data.product_number);
                //选中下拉框中的option
                $("#supplier_id").find("option[value=" + data.data.supplier_id + "]").prop("selected", true);
                $("#product_type_id").find("option[value=" + data.data.product_type_id + "]").prop("selected", true);
                //重新渲染下拉框
                $("#name").val(data.data.name);
                $("#price").val(data.data.price);
                $("#inprice").val(data.data.inprice);
                $("#unit").val(data.data.unit);
                $("#spec").val(data.data.spec);
                $("#remark").val(data.data.remark);
            }
        });
    }
    //layui配置
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
            var $ = layui.$
                , form = layui.form;
            //监听提交
            form.on('submit(entity-submit)', function (data) {
                var field = data.field; //获取提交的字段
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                //获取id，若无则改为0
                var id = $("#entityId").val();
                if (id == null || id == "") id = 0;
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.ajax({
                    url: parent.entityURL,
                    type: 'post',
                    dataType: 'json',
                    async: false,//设置阻断
                    data: {
                        "id": id,
                        "product_number": $("#product_number").val(),
                        "supplier_id": $("#supplier_id").val(),
                        "product_type_id": $("#product_type_id").val(),
                        "name": $("#name").val(),
                        "price": $("#price").val(),
                        "inprice": $("#inprice").val(),
                        "spec": $("#spec").val(),
                        "unit": $("#unit").val(),
                        "remark": $("#remark").val(),
                    },
                    success: function (data) {
                        parent.layer.msg(data.msg, {icon: data.icon, zIndex: layer.zIndex + 100});
                    }
                });
                parent.layui.table.reload('entityTable'); //重载表格
                parent.layer.close(index); //再执行关闭
            });
        }
    )
</script>
</body>
</html>